package com.lagou.cl.intercepetor;

import com.lagou.cl.controller.vo.BaseResult;
import com.lagou.cl.pojo.UserInfo;
import com.lagou.cl.util.JackSonUtil;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author 程  林
 * @date 2020-04-18 18:11
 * @description //
 * @since V1.0.0
 */
@Component
public class MyInterceptor implements HandlerInterceptor {


	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json;charset=utf-8");

		System.out.println("\n请求地址：" + request.getRequestURI());

		HttpSession session = request.getSession();
		UserInfo account = (UserInfo) session.getAttribute("account");
		if (account == null) {
			System.out.println("用户未登录");
			BaseResult baseResult = new BaseResult("9999", "用户未登录", false);
			response.getWriter().write(JackSonUtil.getJsonStr(baseResult));
			return false;
		}

		return true;
	}

}
